package imgshare.common.listener;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import imgshare.common.event.ServerAnnouncementCreatedEvent;
import imgshare.common.sse.SseEmitters;
import imgshare.model.Notification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ServerAnnouncementCreatedListener {

    @Autowired
    SseEmitters emitters;
    private static final Log log = LogFactory.get();

    @Async
    @EventListener(ServerAnnouncementCreatedEvent.class)
    public void handleServerAnnouncementCreated(ServerAnnouncementCreatedEvent event) {
        Notification notification = event.getNotification();
        log.info("为{}个用户发送了系统消息[{}]",emitters.count(),notification.getContent());
        emitters.batchSendMessage(notification);
    }
}
